package com.ckd.flyingtrip.shared.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.shared.bean.SharedCarOrderBean;

import java.util.List;

public class SharedCarOrderAdapter extends RecyclerView.Adapter<SharedCarOrderAdapter.MyHolder> {

    private Context context;
    private List<SharedCarOrderBean.DataBean.RowsBean> list;

    public OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public SharedCarOrderAdapter(Context context, List<SharedCarOrderBean.DataBean.RowsBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_sharedcar_order_recyclerview, null));
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.textView1.setText(list.get(position).getId());
        holder.textView2.setText(list.get(position).getBeginTime());
        holder.textView3.setText(list.get(position).getEndTime());
        holder.textView4.setText(list.get(position).getTripTimes() + "s");
        holder.textView5.setText(list.get(position).getTripDistance() + "m");
        holder.textView6.setText("¥" + list.get(position).getStartPrice());
        if (list.get(position).getFencePrice() == null)
            holder.ll7.setVisibility(View.GONE);
        else
            holder.textView7.setText("¥" + list.get(position).getFencePrice());
        if (list.get(position).getTimePrice() == null)
            holder.ll8.setVisibility(View.GONE);
        else
            holder.textView8.setText("¥" + list.get(position).getTimePrice());
        if (list.get(position).getDistancePrice() == null)
            holder.ll9.setVisibility(View.GONE);
        else
            holder.textView9.setText("¥" + list.get(position).getDistancePrice());
        holder.textView10.setText("¥" + list.get(position).getTotalPrice());
        holder.textView11.setText(list.get(position).getPayTranTime());
        holder.textView12.setText(list.get(position).getTripType().equals("0") ? "直接扫码订单" : "预约订单");
        holder.textView13.setText(list.get(position).getStartAddress());
        if (list.get(position).getEndAddress() == null)
            holder.ll14.setVisibility(View.GONE);
        else
            holder.textView14.setText(list.get(position).getEndAddress());
        holder.textView15.setText(list.get(position).getPayState().equals("0") ? "未支付" : "已支付");

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout, ll7, ll8, ll9, ll14;
        TextView textView1, textView2, textView3, textView4, textView5, textView6, textView7, textView8, textView9,
                textView10, textView11, textView12, textView13, textView14, textView15;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_ll);
            ll7 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_ll7);
            ll8 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_ll8);
            ll9 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_ll9);
            ll14 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_ll14);
            textView1 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView1);
            textView2 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView2);
            textView3 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView3);
            textView4 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView4);
            textView5 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView5);
            textView6 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView6);
            textView7 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView7);
            textView8 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView8);
            textView9 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView9);
            textView10 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView10);
            textView11 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView11);
            textView12 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView12);
            textView13 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView13);
            textView14 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView14);
            textView15 = itemView.findViewById(R.id.item_sharedcar_order_recyclerview_textView15);
        }
    }
}
